/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.dusty.fs.ls;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import ru.dusty.fs.filesystem.data.Block;
import ru.dusty.fs.filesystem.data.BlockType;
import ru.dusty.fs.filesystem.data.MetaData;
import ru.dusty.fs.filesystem.utils.Utils;

/**
 *
 * @author Dusty
 */
public class LsFs {
    
    private static final String NAME = "ls";
    
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy 'at' HH:mm:ss");
    
    public LsFs() {
        
    }
    
    public int ls(String[] args) {
        if (args.length < 1) {
            System.out.println(NAME + ": missing operand after \"" + NAME + "\"");
            System.out.println(NAME + ": use \"" + NAME + " root path\"");
            return 1;
        }
        
        String root = args[0];
        String path = (args.length > 1) ? args[1] : "";

        List<String> paths = Utils.parsePath(path);
        
        try {
            if (!Utils.isFsFormatted(root)) {
                System.out.println(NAME + ": File system error.");
                System.out.println(NAME + ": Use the command \"format root\"");
                return 5;
            }
            
            Block block = Utils.getFirstBlock(root, paths);
            
            if (block == null) {
                System.out.println(NAME + ": Cannot access " + path
                        + ": No such file or directory.");
                return 4;
            }
            
            MetaData data = new MetaData();
            block.readMetaData(data);
            if (data.getType().equals(BlockType.FILE)) {
                block.close();
                System.out.println("Name: " + data.getName()
                        + "; Size: " + data.getSize() + " byte (" + data.getBlockCount()
                        + " blocks); Date: " + dateFormat.format(data.getLastUpdDate()));
            } else {
                System.out.println("Name: " + data.getName()
                        + "; Elements: " + data.getSize()
                        + "; Size: " + data.getBlockCount() + " blocks"
                        + "; Date: " + dateFormat.format(data.getLastUpdDate()));

                List<Integer> listOfNums = Utils.getContainedBlocks(root, block);
                if (listOfNums.isEmpty()) System.out.println("Folder is empty.");
                else System.out.println("Contents of a folder:");
                
                block.close();
                for (Integer num: listOfNums) {
                    block = new Block(new File(root + File.separator + num), "r");
                    block.readMetaData(data);
                    System.out.print(data.getName() + " - " + data.getType());
                    if (data.getType().equals(BlockType.FILE))
                        System.out.print("; Size: " + data.getSize() + " byte ("
                        + data.getBlockCount() + " blocks)");
                    else 
                        System.out.print("; Elements: " + data.getSize() 
                                + "; Size: " + data.getBlockCount() + " blocks");
                    System.out.println("; Date: " + dateFormat.format(data.getLastUpdDate()));
                    block.close();
                }
            }
        } catch (FileNotFoundException ex) {
            System.out.println(NAME + ": Could not find the file block.");
            System.out.println(NAME + ": " + ex.getMessage());
            return 2;
        } catch (IOException ex) {
            System.out.println(NAME + ": Error occurred while reading the file block.");
            System.out.println(NAME + ": " + ex.getMessage());
            return 3;
        }

        return 0;
    }
    
}
